package com.tencent.wxcloudrun.manager;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import javax.annotation.Resource;

import com.tencent.wxcloudrun.enums.CardApplyStatEnum;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tencent.wxcloudrun.domain.TStoreCard;
import com.tencent.wxcloudrun.domain.TUserCardApply;
import com.tencent.wxcloudrun.dto.StoreCardListResDTO;
import com.tencent.wxcloudrun.service.*;
import com.tencent.wxcloudrun.vo.request.card.*;
import com.tencent.wxcloudrun.vo.response.card.*;

import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;

/**
 * @Author ZhaoZiYin
 * @Date 2025/4/22
 * @Description 商家管理端
 */
@Service
@Slf4j
public class StoreCardManager {

    @Resource
    private TUserService userService;

    @Resource
    private TStoreCardService storeCardService;

    @Resource
    private TUserCardService userCardService;

    @Resource
    private TUserCardApplyService userCardApplyService;

    @Resource
    private TUserCardRecordService userCardRecordService;

    /**
     * @Author: ZhaoZiYin
     * @Date: 2025/4/22
     * @Params: [reqVo]
     * @Return: StoreCardAddResVo
     * @Description: 新增卡片
     */
    @Transactional(rollbackFor = Exception.class)
    public StoreCardAddResVo addCard(StoreCardAddReqVo reqVo) {
        userService.checkAdmin(reqVo.getUserId());
        storeCardService.save(reqVo.build());
        return null;
    }

    /**
     * @Author: ZhaoZiYin
     * @Date: 2025/4/22
     * @Params: [reqVo]
     * @Return: StoreCardDeleteResVo
     * @Description: 删除卡片
     */
    @Transactional(rollbackFor = Exception.class)
    public StoreCardDeleteResVo deleteCard(StoreCardDeleteReqVo reqVo) {
        userService.checkAdmin(reqVo.getUserId());
        storeCardService.deleteCard(reqVo.getUserId(), reqVo.getCardId());
        return null;
    }

    /**
     * @Author: ZhaoZiYin
     * @Date: 2025/4/22
     * @Params: [reqVo]
     * @Return: StoreCardListResVo
     * @Description: 查询卡片列表
     */
    public StoreCardListResVo listCard(StoreCardListReqVo reqVo) {
        StoreCardListResVo resVo = new StoreCardListResVo();
        List<TStoreCard> cardList = storeCardService.listCard(reqVo);
        if (!CollectionUtils.isEmpty(cardList)) {
            List<StoreCardListResDTO> cardDTOList = new ArrayList<>(cardList.size());
            for (TStoreCard storeCard : cardList) {
                StoreCardListResDTO resDTO = new StoreCardListResDTO();
                resDTO.setCardId(storeCard.getId());
                resDTO.setName(storeCard.getName());
                resDTO.setMemo(storeCard.getMemo());
                resDTO.setImgUrl(storeCard.getImgUrl());
                resDTO.setApplyFlag(storeCard.getApplyFlag());
                resDTO.setOrderNo(storeCard.getOrderNo());
                cardDTOList.add(resDTO);
            }
            resVo.setCardList(cardDTOList);
        }
        return resVo;
    }

    /**
     * @Author: ZhaoZiYin
     * @Date: 2025/5/13
     * @Params: [reqVo]
     * @Return: UserCardApproveApplyResVo
     * @Description: 审批申请卡片
     */
    @Transactional(rollbackFor = Exception.class)
    public StoreCardApproveApplyResVo approveApply(StoreCardApproveApplyReqVo reqVo) {
        // 更新申请状态
        TUserCardApply apply = userCardApplyService.approveApply(reqVo.getUserId(), reqVo);

        // 如果通过 则更新用户卡片与卡片记录
        if (Objects.nonNull(apply) && reqVo.getApplyStatus().equals(CardApplyStatEnum.PASS.getValue())) {
            userCardService.saveCard(apply.getUserId(), apply.getCardId(), apply.getCount());
            userCardRecordService.saveCardRecord(apply);
        }
        return null;
    }

    /**
     * @Author: ZhaoZiYin
     * @Date: 2025/5/27
     * @Params: [reqVo]
     * @Return: StoreCardQueryCountResVo
     * @Description: 查询卡片总数
     */
    public StoreCardQueryCountResVo queryCount(StoreCardQueryCountReqVo reqVo) {
        StoreCardQueryCountResVo resVo = new StoreCardQueryCountResVo();
        resVo.setCountList(userCardService.queryCount());
        return resVo;
    }

    /**
     * @Author: ZhaoZiYin
     * @Date: 2025/5/27
     * @Params: [reqVo]
     * @Return: StoreCardQueryUserCountResVo
     * @Description: 查询卡片总数
     */
    public IPage<StoreCardQueryUserCountResVo> queryUserCount(StoreCardQueryUserCountReqVo reqVo) {
        Page<StoreCardQueryUserCountResVo> page = new Page<>(reqVo.getCurrent(), reqVo.getSize());
        List<Long> userIdList = new ArrayList<>();
        if (StringUtils.hasLength(reqVo.getKeyWord())) {
            userIdList = userService.searchByName(reqVo.getKeyWord());
            if (CollectionUtils.isEmpty(userIdList)) {
                return page;
            }
        }
        List<StoreCardQueryUserCountResVo> resVoList = userCardService.queryUserCount(page, reqVo.getCardId(), userIdList);
        page.setRecords(resVoList);
        return page;
    }

}
